page.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use client';
  2. import { useState, useEffect, useRef } from 'react';
  3. import { useRouter, useParams } from 'next/navigation';
  4. import Link from 'next/link';
  5. import { fetchApi } from '@/lib/utils/client';
  6. import { useStudioContext } from '@/app/studio/context';
  7. import { useAlertConfigContext } from '../../context';
  8. import { Separator } from '@/components/ui/separator';
  9. import AlertPreviewPanel from '../../_components/AlertPreviewPanel';
  10. import AlertFormPanel from '../../_components/AlertFormPanel';
  11. import { createEmptyForm } from '../../types';
  12. import type { FormState, PendingFiles } from '../../types';
  13. import type { AlertConfigItem } from '@/types/response/donation/alertConfig';
  14. export default function AlertEditPage()
  15. {
  16. const router = useRouter();
  17. const { id } = useParams<{ id: string }>();
  18. const numericId = parseInt(id);
  19. const { channelID, memberID } = useStudioContext();
  20. const { items, widgetToken, loading, setSaving } = useAlertConfigContext();
  21. const [editingItem, setEditingItem] = useState<AlertConfigItem|null>(null);
  22. const [form, setForm] = useState<FormState>(createEmptyForm());
  23. const [formInitialized, setFormInitialized] = useState(false);
  24. const [pendingFiles, setPendingFiles] = useState<PendingFiles>({ image: null, sound: null });
  25. const [localSaving, setLocalSaving] = useState(false);
  26. const iframeRef = useRef<HTMLIFrameElement>(null);
  27. const formRef = useRef<FormState>(form);
  28. formRef.current = form;
  29. // ── items 로드 후 form 초기화 ────────────────────
  30. useEffect(() => {
  31. if (formInitialized || items.length === 0) {
  32. return;
  33. }
  34. const found = items.find(item => item.id === numericId);
  35. if (found) {
  36. setEditingItem(found);
  37. const { id, ...rest } = found;
  38. void id;
  39. setForm(rest);
  40. setFormInitialized(true);
  41. } else if (!loading) { // 준비 완료인데 못 찾음
  42. alert('알림 설정을 찾을 수 없습니다.');
  43. router.push('/studio/donation/alert/list');
  44. }
  45. }, [items, loading, numericId, formInitialized, router]);
  46. // ── blob URL cleanup ─────────────────────────────
  47. const cleanupBlobUrls = (f: FormState) => {
  48. if (f.imageUrl?.startsWith('blob:')) {
  49. URL.revokeObjectURL(f.imageUrl);
  50. }
  51. if (f.soundUrl?.startsWith('blob:')) {
  52. URL.revokeObjectURL(f.soundUrl);
  53. }
  54. };
  55. useEffect(() => {
  56. return () => {
  57. cleanupBlobUrls(formRef.current);
  58. };
  59. }, []);
  60. // ── 폼 → iframe 미리보기 동기화 ─────────────────
  61. useEffect(() => {
  62. if (!iframeRef.current?.contentWindow) {
  63. return;
  64. }
  65. iframeRef.current.contentWindow.postMessage({
  66. type: 'ALERT_PREVIEW',
  67. config: form,
  68. }, window.location.origin);
  69. }, [form]);
  70. // ── 폼 필드 변경 ────────────────────────────────
  71. const handleFormChange = <K extends keyof FormState>(field: K, value: FormState[K]) => {
  72. setForm(prev => {
  73. if ((field === 'imageUrl' || field === 'soundUrl') && typeof prev[field] === 'string' && (prev[field] as string).startsWith('blob:')) {
  74. URL.revokeObjectURL(prev[field] as string);
  75. }
  76. return { ...prev, [field]: value };
  77. });
  78. if (field === 'imageUrl' && value === null) {
  79. setPendingFiles(prev => ({ ...prev, image: null }));
  80. }
  81. if (field === 'soundUrl' && value === null) {
  82. setPendingFiles(prev => ({ ...prev, sound: null }));
  83. }
  84. };
  85. // ── 파일 업로드 헬퍼 ─────────────────────────────
  86. const uploadFile = async (file: File, type: 'image'|'sound'): Promise<string> => {
  87. const formData = new FormData();
  88. formData.append('file', file);
  89. formData.append('type', type);
  90. formData.append('channelID', channelID!.toString());
  91. const res = await fetchApi<{ url: string }>('/api/studio/donation/alert/config/upload', {
  92. method: 'POST',
  93. body: formData,
  94. });
  95. return res.data?.url ?? '';
  96. };
  97. // ── 저장 ─────────────────────────────────────────
  98. const handleSave = async () => {
  99. if (!channelID || !editingItem) {
  100. return;
  101. }
  102. if (!form.message.trim()) {
  103. alert('메시지를 입력해 주세요.');
  104. return;
  105. }
  106. if (form.amount < 1) {
  107. alert('금액은 1원 이상이어야 합니다.');
  108. return;
  109. }
  110. if (form.displayDurationSec < 1) {
  111. alert('노출 시간은 1초 이상이어야 합니다.');
  112. return;
  113. }
  114. setLocalSaving(true);
  115. setSaving(true);
  116. try {
  117. let finalImageUrl = form.imageUrl;
  118. let finalSoundUrl = form.soundUrl;
  119. if (pendingFiles.image) {
  120. finalImageUrl = await uploadFile(pendingFiles.image, 'image');
  121. }
  122. if (pendingFiles.sound) {
  123. finalSoundUrl = await uploadFile(pendingFiles.sound, 'sound');
  124. }
  125. const item = {
  126. id: editingItem.id,
  127. ...form,
  128. imageUrl: finalImageUrl,
  129. soundUrl: finalSoundUrl,
  130. popupEffect: form.popupEffect || null,
  131. textEffect: form.textEffect || null,
  132. nicknameFontFamily: form.nicknameFontFamily || null,
  133. amountFontFamily: form.amountFontFamily || null,
  134. messageFontFamily: form.messageFontFamily || null,
  135. };
  136. await fetchApi('/api/studio/donation/alert/config/batch', {
  137. method: 'POST',
  138. body: { channelID, memberID, items: [item], deleteIDs: [] },
  139. });
  140. cleanupBlobUrls(form);
  141. alert('수정되었습니다.');
  142. } catch (err) {
  143. alert(err instanceof Error ? err.message : '저장에 실패했습니다.');
  144. } finally {
  145. setLocalSaving(false);
  146. setSaving(false);
  147. }
  148. };
  149. // ── 취소 ─────────────────────────────────────────
  150. const handleCancel = () => {
  151. cleanupBlobUrls(form);
  152. router.push('/studio/donation/alert/list');
  153. };
  154. // ── 로딩 중 ──────────────────────────────────────
  155. if (!formInitialized) {
  156. return <div className="alert-config__loading">준비 중...</div>;
  157. }
  158. return (
  159. <>
  160. <div className="studio-page__title-row">
  161. <h1 className="studio-page__title">후원 알림 수정</h1>
  162. <Link href="/studio/donation/alert/list" className="alert-config__btn alert-config__btn--sm">< 목록으로</Link>
  163. </div>
  164. <div className='pt-5 pb-5'>
  165. <Separator orientation="horizontal" />
  166. </div>
  167. <div className="alert-config__layout">
  168. <AlertPreviewPanel
  169. widgetToken={widgetToken}
  170. iframeRef={iframeRef}
  171. />
  172. <Separator orientation="vertical" />
  173. <AlertFormPanel
  174. form={form}
  175. editingItem={editingItem}
  176. saving={localSaving}
  177. pendingFiles={pendingFiles}
  178. onFileSelect={(file, type) => {
  179. const previewUrl = URL.createObjectURL(file);
  180. if (type === 'image') {
  181. setPendingFiles(prev => ({ ...prev, image: file }));
  182. handleFormChange('imageUrl', previewUrl);
  183. } else {
  184. setPendingFiles(prev => ({ ...prev, sound: file }));
  185. handleFormChange('soundUrl', previewUrl);
  186. }
  187. }}
  188. onFormChange={handleFormChange}
  189. onSave={handleSave}
  190. onCancel={handleCancel}
  191. />
  192. </div>
  193. </>
  194. );
  195. }